<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Controller extends CI_Controller {
	
	protected $_data = array();
	protected $is_login;
	protected $loginer;
	protected $p;
	protected $tbl;	
	protected $mid = 0;
    function __construct(){
        parent::__construct();
    	date_default_timezone_set('PRC');
    	session_start();

    	$is_first_access = $this->session->userdata('is_first_access');
    	$this->_data['is_first_access'] = $is_first_access;
    	if(empty($is_first_access)){
       	    $this->session->set_userdata('is_first_access',mktime());
    	}

    	//取得所有表
    	$this->tbl = (object)config_item('tbl');
    	
    	//加载通用模型
    	$this->load->model('common_model','cm');
    	
        //用户是否登录
        $loginer = empty($_SESSION['loginer']) ? null : $_SESSION['loginer'];
        $is_login = !empty($loginer) ? true : false;
        
        $this->is_login = $is_login; 
        
        //登录者信息
        if($is_login){
        	$this->_data['loginer'] = $loginer; 
        	$this->loginer = $loginer;

        }

        //站点地址
        $this->_data['base_url'] = base_url();
        $this->_data['static_url'] = base_url().'admin/statics';
        
        //通用用户数据处理
    	$user_data = array();
    	$arr_r = array_merge($_GET,$_POST);
    	foreach ($arr_r as $k=>$v){
    		if(is_string($v)){
	    		$v = trim($v);
    		}
    		$user_data[$k] = $v;
    	}

    	$this->p = $user_data;
		if(empty($this->p['m']) || !empty($this->p['m'])  && ($this->p['m'] != 'login' && $this->p['m'] != 'login_do')){
			if($this->is_ajax_request()){
				$this->check_ajax_login();
			}else{
				$this->check_page_login();
			}
		}


		//统一管理当前导航
		$c = $this->p['c'];
		$m = $this->p['m'];
		
		if($c == 'model' && $m=='index' || $c == 'model' && $m=='view_attr'){
			$this->_data['tm-model'] = ' c ';
			$this->_data['sm-model-index'] = ' c ';
		}elseif($c == 'cat'){
			$this->_data['tm-cat'] = ' c ';
			$this->_data['sm-cat-index'] = ' c ';
		}elseif($c == 'data'){
			$this->_data['tm-data'] = ' c ';
			if($m == 'add') $this->_data['sm-data-add'] = ' c ';
			if($m == 'index') $this->_data['sm-data-index'] = ' c ';
		}elseif($c == 'block'){
			$this->_data['tm-block'] = ' c ';
			if($m == 'add') $this->_data['sm-block-add'] = ' c ';
			if($m == 'index') $this->_data['sm-block-index'] = ' c ';
			
		}elseif($c == 'product'){
			$this->_data['tm-product'] = ' c ';
			if($m == 'add') $this->_data['sm-product-add'] = ' c ';
			if($m == 'index') $this->_data['sm-product-index'] = ' c ';
		}

		//上次访问路径
		if(!empty($_SERVER['HTTP_REFERER'])){
			$this->_data['pre_url'] = $_SERVER['HTTP_REFERER'];
		}

		if(!empty($_SESSION['mid'])){
			$this->mid = $_SESSION['mid'];
			$this->_data['mid'] = $_SESSION['mid'];
		}


		$this->_data['p'] = $this->p;
    }
    
    function page_url(){
        $p = $this->p;
        unset($p['per_page']);
    	if(empty($p) || !is_array($p)) return site_url();
    	$arr = array();
    	foreach ($p as $k=>$v){
			$arr[] = $k .'='.$v;	    	
    	}
    	return site_url(implode('&', $arr));
    }
    
    
    function check_page_login(){
    	if(!$this->is_login){
    		redirect(site_url('c=member&m=login'));
    		exit;
    	}
    }
    
    function check_ajax_login(){
    	if(!$this->is_login){
    		ejd('unlogin', '', site_url('c=member&m=login'));
    	}
    }

    function is_ajax_request(){
		return !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
    }

    function page_to_index(){
  		redirect(site_url());
   		exit;
    }
    
    
    function splash($href,$msg){
    	$this->_data['msg'] = $msg;
    	$this->_data['href'] = $href;
    	$this->load->view('splash',$this->_data);
    }
    
    function upload(){
    	require_once LIB_PATH.'thumb/ThumbLib.inc.php';

		define('PUBLIC_DIR',dirname(__FILE__).'/public');
		define('IMG_DIR',PUBLIC_DIR.'/images');
		define('IMG_PATH','public/images');

		if(empty($_FILES['Filedata']['name'])) die;

		$arr_filename = explode('.', $_FILES['Filedata']['name']);
		$extname = $arr_filename[count($arr_filename)-1];
		$extname = strtoupper($extname);

		if(!in_array($extname, array('JPG','GIF','PNG'))) die;

		$rname = rand_filename();
		$ofname = 'O_'.$rname.'.'.$extname;
		$tfname = 'B_'.$rname.'.'.$extname;
		$lfname = 'L_'.$rname.'.'.$extname;

		$opath = IMG_PATH.'/'.$ofname;
		$tpath = IMG_PATH.'/'.$tfname;
		$lpath = IMG_PATH.'/'.$lfname;

		$tempFile = $_FILES['Filedata']['tmp_name'];

		$conf_thumb = config_item('thumb');

		$thumb = PhpThumbFactory::create($tempFile);
		$thumb->resize($conf_thumb['w']);
		$thumb->save($tpath);

		$thumb = PhpThumbFactory::create($tempFile);
		$thumb->adaptiveResize($conf_thumb['lw'], $conf_thumb['lh']);
		$thumb->save($lpath);

		@move_uploaded_file($tempFile, $opath);

		$id = $this->cm->save('images',array(
			'o'=>$opath,
			't'=>$tpath,
			'l'=>$lpath
		));

		$base_url = config_item('base_url');
		echo json_encode(array(
			'spath'=>$base_url.$lpath,
			'id'=>$id
		)); 

		exit;

		
    }	
    
    
    function findnone(){
    	$this->load->view('findnone',$this->_data);
    }
    
	function avatar(){
        require_once LIB_PATH.'thumb/ThumbLib.inc.php';
		$c = $this->config->item('avatar');
		
		$arr_filename = explode('.', $_FILES['Filedata']['name']);
		$extname = $arr_filename[count($arr_filename)-1];
		$new_filename = rand_filename().'.'.$extname; 

		$small_file_path = SAVATAR_DIR.$new_filename;
		$mdl_file_path = MAVATAR_DIR.$new_filename;
		$file_path = AVATAR_DIR.$new_filename;
		
		$upload_small_file_path = 'upload/avatar/s/'.$new_filename;
		$upload_mdl_file_path = 'upload/avatar/m/'.$new_filename;
		$upload_file_path = 'upload/avatar/'.$new_filename;
		
		$data = array();
		$tempFile = $_FILES['Filedata']['tmp_name'];
		
		$thumb = PhpThumbFactory::create($tempFile);
		$thumb->adaptiveResize($c['sw'], $c['sh']);
		$thumb->save($small_file_path);
		
		$thumb = PhpThumbFactory::create($tempFile);
		$thumb->adaptiveResize($c['mw'], $c['mh']);
		$thumb->save($mdl_file_path);
		
		$thumb = PhpThumbFactory::create($tempFile);
		$thumb->resize($c['bw']);
		$thumb->save($file_path);

		$data['small_path'] = $upload_small_file_path;
		$data['mdl_path'] = $upload_mdl_file_path;
		$data['path'] = $upload_file_path;
		
		$id = $this->cm->save($this->tbl->avatar,$data);
		
		echo json_encode(array('id'=>$id,'spath'=>config_item('base_url').$upload_small_file_path)); 
		
	}
    
	function get_config($k){
		$r = $this->cm->row();

		$print_r($r);

	}

	function get_kv($k){
		return $this->cm->get_kv($k);
	}

	function set_kv($k,$v){
		return $this->cm->set_kv($k,$v);		
	}

	function del_kv($k){
		return $this->cm->del_kv($k);		
	}
    
    
}
